<template>
  <PanerGrid @onSubmit="handleSubmit" @onReset="handleReset">
    <template v-slot:search>
      <div class="d-group-items d-layout-grid d-layout-grid-4">
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">名称</label>
            </div>
            <div class="d-form-el">
              <input v-model="search.originName" class="d-form-input" />
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">类型</label>
            </div>
            <div class="d-form-el">
              <input v-model="search.ext" class="d-form-input" placeholder="请输入文件后缀" />
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">状态</label>
            </div>
            <div class="d-form-el">
              <select v-model="search.status" class="d-form-select">
                <option v-for="item in archiveStatusList" :key="item.id" :value="item.id">{{ item.text }}</option>
              </select>
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">描述</label>
            </div>
            <div class="d-form-el">
              <input v-model="search.description" class="d-form-input" />
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">标签</label>
            </div>
            <div class="d-form-el">
              <input v-model="search.labels" class="d-form-input" type="text" />
            </div>
          </div>
        </div>
        <div class="d-grid-item">
          <FormCalendar
            v-model="search.createTime"
            label="创建时间"
            labelWidth="80px"
            class="d-form-group-flex"
          ></FormCalendar>
        </div>
        <div class="d-grid-item">
          <FormCalendar
            v-model="search.updateTime"
            label="修改时间"
            labelWidth="80px"
            class="d-form-group-flex"
          ></FormCalendar>
        </div>
        <div v-if="false" class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">创建人</label>
            </div>
            <div class="d-form-el">
              <input class="d-form-input" />
            </div>
          </div>
        </div>
        <div v-if="false" class="d-grid-item">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">修改人</label>
            </div>
            <div class="d-form-el">
              <input class="d-form-input" />
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-slot:header>
      <div class="d-paner-caption">
        <span class="d-caption-text">文件列表</span>
        <div class="d-bar-tools d-bar-tools-condensed">
          <span class="d-tools-item"><a class="d-btn d-btn-plain-sign" :href="getMicroPath('#/dfile/upload')">上传</a></span>
          <span @click="handleDelChecked" class="d-tools-item"><button class="d-btn d-btn-plain-sign" type="button">删除</button></span>
        </div>
      </div>
      <div class="d-bar-tools d-bar-tools-right d-bar-tools-condensed">
        <a @click="handleReload" class="d-tools-item"><span class="d-btn d-btn-plain-sign d-btn-square-sm d-btn-flex"><RefreshIcon /></span></a>
      </div>
    </template>
    <template v-slot:body>
      <Grid v-model="list" ref="gridList" :request="gridRequest" :pageSize="15">
        <GridColumn :width="48" type="checkbox" />
        <GridColumn :width="60" type="index" label="序号" />
        <GridColumn field="name" label="名称">
          <template v-slot:body="scope">
            <a :href="getMicroPath('#/dfile/item/' + scope.id)" class="d-text-theme d-text-linker">{{ scope.originName }}</a>
          </template>
        </GridColumn>
        <GridColumn field="size" :width="140" format="capacity" label="大小(Byte)" />
        <GridColumn field="ext" :width="90" label="文件类型" />
        <GridColumn field="status" label="状态" format="select" :select="archiveStatusList" width="80" />
        <GridColumn field="updateTime" label="修改时间" format="datetime|yyyy-MM-dd HH:mm:ss" width="160" />
        <!-- <GridColumn field="updateUser" label="修改人" /> -->
        <GridColumn field="createTime" label="创建时间" format="datetime|yyyy-MM-dd HH:mm:ss" width="160" />
        <!-- <GridColumn field="createUser" label="创建人" /> -->
        <GridColumn field="action" :width="120" label="操作">
          <template v-slot:body="scope">
            <div class="d-bar-tools d-bar-tools-condensed d-grid-actions">
              <a :href="getMicroPath('#/dfile/item/' + scope.id)" class="d-tools-item d-text-theme" title="查看"><EditIcon size="sm" /></a>
              <a @click="handleUploadItem(scope)" class="d-tools-item d-text-theme" title="部署"><OrganizeIcon size="sm" /></a>
              <a @click="handleDelItem(scope)" class="d-tools-item d-text-theme"><DeleteIcon size="sm" /></a>
            </div>
          </template>
        </GridColumn>
      </Grid>
      <Dialoger v-model="isShowDeploy" :onConfirm="handleSubmitDeploy" :dialogCls="'d-paner-sm'" title="目录选择">
        <template v-slot:body>
          <div class="d-row">
            <div class="d-form-group d-form-group-flex">
              <div class="d-form-label d-text-right">
                <label class="label-text">目录</label>
              </div>
              <div class="d-form-el">
                <select v-model="deploy.dir" class="d-form-select">
                  <option v-for="item in deployList" :key="item.id" :value="item.id">{{ item.text }}</option>
                </select>
              </div>
            </div>
            <div class="d-form-group d-form-group-flex">
              <div class="d-form-label d-text-right">
                <label class="label-text">解压</label>
              </div>
              <div class="d-form-el">
                <select v-model="deploy.decompression" class="d-form-select">
                  <option v-for="item in state.judgeOptions" :key="item.id" :value="item.id">{{ item.text }}</option>
                </select>
              </div>
            </div>
          </div>
        </template>
      </Dialoger>
    </template>
  </PanerGrid>
</template>

<script>
import { PanerGrid } from '@daelui/vdog/dist/pages'
import { Grid, GridColumn, Dialoger, FormCalendar, Pager } from '@daelui/vdog/dist/components'
import { RefreshIcon, EditIcon, DeleteIcon, SeeIcon, OrganizeIcon } from '@daelui/vdog/dist/icons'
import { zh as locale } from '@daelui/dogjs/dist/locale'
import fSer, { api } from '../../service/action/dfile.action.js'
import poSer from '@/service/action/pool.action.js'
import { getter, state } from '../../service/stock/index.js'

export default {
  components: {
    PanerGrid,
    Grid,
    GridColumn,
    Dialoger,
    RefreshIcon,
    EditIcon,
    DeleteIcon,
    SeeIcon,
    OrganizeIcon,
    FormCalendar,
    Pager
  },

  data () {
    return {
      state,
      // 搜索条件
      search: {
        name: '',
        productId: '',
        type: '',
        status: '',
        labels: '',
        content: '',
        createTime: '',
        updateTime: ''
      },
      list: [],
      // 表格请求配置
      gridRequest: {
        url: api.queryListPage.url, // 请求地址
        method: api.queryListPage.method, // 请求方式
        params: {} // 请求参数
      },
      // 数据源类型
      resourceTypeList: getter.resourceTypeList(),
      // 状态
      archiveStatusList: getter.statusList(),
      // 部署选择窗口
      isShowDeploy: false,
      deploy: {
        item: null,
        dir: '',
        decompression: 1
      },
      deployList: []
    }
  },

  methods: {
    /**
     * @function 查询事件处理
    */
    handleSubmit () {
      this.$refs.gridList.reload(this.search)
    },

    /**
     * @function 重置事件
    */
    handleReset () {
      this.search = {
        name: '',
        productId: '',
        type: '',
        status: '',
        labels: '',
        content: '',
        createTime: '',
        updateTime: ''
      }
    },

    /**
     * @function 删除选定的数据
    */
    handleDelChecked () {
      let items = this.$refs.gridList.getChecked()
      this.removeItems(items)
    },

    /**
     * @function 删除单条数据
    */
    handleDelItem (item) {
      this.removeItems([item])
    },

    /**
     * @function 删除多条数据
    */
    removeItems (items) {
      items = Array.isArray(items) ? items : []
      if (items.length < 1) {
        Dialoger.warn(locale.list.selectOne)
        return false
      }
      Dialoger.confirm(locale.message.confirmDelete).then(b => {
        if (!b) {
          return false
        }
        items = items.map(item => item.id)
        fSer.removeItem(items).then(result => {
          this.$refs.gridList.reload()
        })
      })
    },

    /**
     * @function 弹出部署选择
    */
    handleUploadItem (item) {
      this.deploy.item = item
      this.isShowDeploy = true
    },

    /**
     * @function 部署
    */
    handleSubmitDeploy () {
      if (!this.deploy.dir) {
        this.$message({
          message: '请选择目录',
          type: 'warning'
        })
        return false
      }
      fSer.deploy({
        pid: this.deploy.dir,
        files: [this.deploy.item.id]
      }).then(result => {
        if (result.success) {
          Dialoger.success(locale.action.success)
        } else {
          Dialoger.danger(locale.action.fail)
        }
      })
    },

    /**
     * @function 重新加载
    */
    handleReload () {
      this.$refs.gridList.reload(this.search)
    }
  },

  mounted () {
    poSer.queryListAllOption().then(list => {
      this.deployList = list
    })
  }
}
</script>

<style scoped>
.d-form-label {
  width: 80px;
}
</style>
